package com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ExceptionCast;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetCategoryManagement;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetFixedStorage;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetProcess;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetProcessDetail;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetCategoryManagementMapper;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetFixedStorageMapper;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetProcessDetailMapper;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.entity.*;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.entity.dto.SmsAssetFixedBorrowApplyDTO;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.entity.dto.SmsAssetFixedBorrowApplyGoodsDTO;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.entity.dto.SmsAssetFixedBorrowApplyMPJDTO;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.entity.dto.SmsAssetFixedBorrowApplyProcessDetailDTO;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.entity.vo.SmsAssetFixedBorrowApplyAllowVO;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.entity.vo.SmsAssetFixedBorrowApplyProcessDetailVO;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.entity.vo.SmsAssetFixedBorrowApplyVO;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.mapper.*;
import com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.service.SmsAssetFixedBorrowApplyService;
import com.xyht.sca_s.student_manage_system.modules.newTips.myEnum.AppTypeEnum;
import com.xyht.sca_s.student_manage_system.modules.newTips.myEnum.RangeTypeEnum;
import com.xyht.sca_s.student_manage_system.modules.newTips.myEnum.TipsTypeEnum;
import com.xyht.sca_s.student_manage_system.modules.newTips.util.NewTipsUtil;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.entity.resp.SmsSearchUserResp;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.asset.constant.AssetBooleanConstant.ASSET_BOOLEAN_FALSE;
import static com.xyht.sca_s.student_manage_system.modules.asset.constant.AssetBooleanConstant.ASSET_BOOLEAN_TRUE;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.constant.AssetAllowStatusConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.constant.AssetApplyTypeConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.constant.AssetBooleanConstant.ASSET_BORROW_BOOLEAN_TRUE;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.constant.AssetObjectTypeConstant.ASSET_BORROW_OBJECT_TYPE_PERSON;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.constant.AssetObjectTypeConstant.ASSET_BORROW_OBJECT_TYPE_ROLE;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.constant.AssetProcessTypeConstant.ASSET_PROCESS_TYPE_FIXED_BORROW_STORAGE;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.constant.AssetStatusConstant.ASSET_STATUS_BORROW;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.constant.AssetStatusConstant.ASSET_STATUS_IN_STORAGE;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedBorrow.constant.MyAssetStatusConstant.MY_ASSET_STATUS_BORROW;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-05-29
 */
@Service
public class SmsAssetFixedBorrowApplyServiceImpl extends ServiceImpl<SmsAssetFixedBorrowApplyMapper, SmsAssetFixedBorrowApply> implements SmsAssetFixedBorrowApplyService {
    @Autowired
    SmsAssetFixedBorrowApplyMapper smsAssetFixedBorrowApplyMapper;
    @Autowired
    SmsAssetProcessDetailMapper smsAssetProcessDetailMapper;
    @Autowired
    SmsAssetCategoryManagementMapper smsAssetCategoryManagementMapper;
    @Autowired
    SmsAssetFixedBorrowApplyGoodsMapper smsAssetFixedBorrowApplyGoodsMapper;
    @Autowired
    SmsAssetProcessMapper smsAssetProcessMapper;
    @Autowired
    SmsAssetFixedBorrowApplyProcessDetailMapper smsAssetFixedBorrowApplyProcessDetailMapper;
    @Autowired
    NewTipsUtil newTipsUtil;
    @Autowired
    CacheUtil cacheUtil;
    @Autowired
    SmsAssetFixedBorrowApplyProcessDetailMPJMapper smsAssetFixedBorrowApplyProcessDetailMPJMapper;
    @Autowired
    SmsAssetFixedBorrowApplyGoodsDetailMapper smsAssetFixedBorrowApplyGoodsDetailMapper;
    @Autowired
    SmsAssetFixedStorageMapper smsAssetFixedStorageMapper;
    @Autowired
    SmsAssetMyAssetsMapper smsAssetMyAssetsMapper;
    @Autowired
    SmsUserMapper smsUserMapper;


    @Transactional
    @Override
    public ResponseResult smsAssetFixedBorrowApplyAdd(SmsAssetFixedBorrowApplyVO applyVO, String user_id) {
        if (isNullOrEmpty(applyVO.getApplyName()) || isNullOrEmpty(applyVO.getApplyDepartment()) || isNullOrEmpty(applyVO.getApplyPerson())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

//        if (applyVO.getGoodsList().size() < 1) {
//            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
//        }
        if (isNullOrEmpty(applyVO.getAssetIdList()) || applyVO.getAssetIdList().size() < 1) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        for (String storageId : applyVO.getAssetIdList()) {
            if (isNullOrEmpty(storageId)) {
                return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
            }
        }


        SmsAssetProcess originProcess1 = smsAssetProcessMapper.selectOne(new QueryWrapper<SmsAssetProcess>()
                .lambda()
                .eq(SmsAssetProcess::getProcessType, ASSET_PROCESS_TYPE_FIXED_BORROW_STORAGE));
        if (originProcess1 == null) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_PROCESS_NOT_SETTING));
        }
        List<SmsAssetProcessDetail> fillProcessList =
                smsAssetProcessDetailMapper.selectList(new QueryWrapper<SmsAssetProcessDetail>()
                        .lambda()
                        .eq(SmsAssetProcessDetail::getProcessId, originProcess1.getId())
                        .eq(SmsAssetProcessDetail::getProcessDetailIsApplicantEdit, true)
                        .eq(SmsAssetProcessDetail::getProcessDetailIsUse, true)
                        .orderByAsc(SmsAssetProcessDetail::getProcessDetailSort));
        if (fillProcessList != null && fillProcessList.size() > 0) {
            if (applyVO.getProcessList() == null || applyVO.getProcessList().size() < 1) {
                return CommonResult.failed(CommonCodeEnum.ASSET_BORROW_ALLOW_USER_NOT_SETTING);
            }
            //判断申请人是否将全部需要编辑流程填写
            List<SmsAssetProcessDetail> filterList = fillProcessList
                    .stream()
                    .filter(process -> {
                                List<SmsAssetFixedBorrowApplyProcessDetailVO> result = applyVO.getProcessList()
                                        .stream()
                                        .filter(fillProcess -> fillProcess.getProcessDetailId().equals(process.getId()))
                                        .collect(Collectors.toList());
                                //匹配的去除  未匹配的留下
                                if (result.size() > 0) {
                                    return false;
                                } else {
                                    return true;
                                }
                            }
                    )
                    .collect(Collectors.toList());
            if (filterList.size() > 0) {
                return CommonResult.failed(CommonCodeEnum.ASSET_BORROW_ALLOW_PROCESS_NOT_FILL);
            }
        }

        //创建申请表
        SmsAssetFixedBorrowApply smsAssetFixedBorrowApply = new SmsAssetFixedBorrowApply();
        BeanUtils.copyProperties(applyVO, smsAssetFixedBorrowApply);
        smsAssetFixedBorrowApply.setApplyUserId(user_id);
        smsAssetFixedBorrowApply.setAllowStatus(ASSET_BORROW_ALLOW_STATUS_WAIT_ALLOW);
        smsAssetFixedBorrowApplyMapper.insert(smsAssetFixedBorrowApply);
        if (isNullOrEmpty(smsAssetFixedBorrowApply.getId())) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.FAIL));
        }
        String apply_id = smsAssetFixedBorrowApply.getId();
        //创建借用申请物品记录
//        for (SmsAssetFixedBorrowApplyGoodsVO goodsVO : applyVO.getGoodsList()) {
//            if (isNullOrEmpty(goodsVO.getAssetName()) || isNullOrEmpty(goodsVO.getAssetNum())
//                    || isNullOrEmpty(goodsVO.getAssetCategory()) || isNullOrEmpty(goodsVO.getAssetModel())) {
//                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
//            }
//            SmsAssetCategoryManagement smsAssetCategoryManagement =
//                    smsAssetCategoryManagementMapper.selectById(goodsVO.getAssetCategory());
//            if (isNullOrEmpty(smsAssetCategoryManagement.getId()) || smsAssetCategoryManagement == null) {
//                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_CATEGORY_NOT_EXIST));
//            }
//            //判断是否为根类别
//            if (smsAssetCategoryManagement.getId().length() < 3) {
//                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_CATEGORY_CAN_NOT_USE_ROOT));
//            }
//
//            //判断数量
//            if (goodsVO.getAssetNum().equals(0)) {
//                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_STORAGE_NUM_ERR));
//            }
//            SmsAssetFixedBorrowApplyGoods smsAssetFixedBorrowApplyGoods = new SmsAssetFixedBorrowApplyGoods();
//            BeanUtils.copyProperties(goodsVO, smsAssetFixedBorrowApplyGoods);
//            smsAssetFixedBorrowApplyGoods.setApplyId(apply_id);
//            smsAssetFixedBorrowApplyGoodsMapper.insert(smsAssetFixedBorrowApplyGoods);
//        }

        //创建借用申请物品记录
        //判断物品是否在库
        List<String> assetIdList = applyVO.getAssetIdList().stream().distinct().collect(Collectors.toList());
        for (String storageId : assetIdList) {
            SmsAssetFixedStorage fixedStorage = smsAssetFixedStorageMapper.selectById(storageId);
            if (fixedStorage.getAssetStatus() != ASSET_STATUS_IN_STORAGE) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_STORAGE_NOT_EXIST));
            }

            SmsAssetFixedBorrowApplyGoodsDetail goodsDetail = new SmsAssetFixedBorrowApplyGoodsDetail();
            goodsDetail.setApplyId(apply_id);
            goodsDetail.setAssetId(storageId);
            smsAssetFixedBorrowApplyGoodsDetailMapper.insert(goodsDetail);
        }


        //创建审批流程
        //主流程

        //流程节点
        List<SmsAssetProcessDetail> originProcessDetailList =
                smsAssetProcessDetailMapper.selectList(new QueryWrapper<SmsAssetProcessDetail>()
                        .lambda()
                        .eq(SmsAssetProcessDetail::getProcessDetailIsUse, ASSET_BOOLEAN_TRUE)
                        .eq(SmsAssetProcessDetail::getProcessId, originProcess1.getId())
                        .orderByAsc(SmsAssetProcessDetail::getProcessDetailSort));
        if (originProcessDetailList == null || originProcessDetailList.size() < 1) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_PROCESS_NOT_SETTING));
        }

        int sort = 1;
        for (SmsAssetProcessDetail process : originProcessDetailList) {
            //判断管理员是否配置初始流程
            if (process.getProcessDetailIsApplicantEdit().equals(ASSET_BOOLEAN_FALSE)) {
                if (isNullOrEmpty(process.getProcessDetailObjectId()) || isNullOrEmpty(process.getProcessDetailObjectType())) {
                    ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.PAYMENT_PROCESS_NOT_SETTING));
                }
            }

            SmsAssetFixedBorrowApplyProcessDetail addDetail =
                    new SmsAssetFixedBorrowApplyProcessDetail();

            addDetail.setApplyId(apply_id);
            addDetail.setProcessDetailId(process.getId());
            addDetail.setProcessDetailIsSign(process.getProcessDetailIsSign());
            addDetail.setProcessDetailIsStamp(process.getProcessDetailIsStamp());
            addDetail.setProcessDetailName(process.getProcessDetailName());
            addDetail.setProcessDetailStatus(ASSET_BORROW_ALLOW_STATUS_WAIT_ALLOW);

            addDetail.setProcessDetailSort(sort++);

            //申请人可编辑流程
            if (process.getProcessDetailIsApplicantEdit().equals(ASSET_BOOLEAN_TRUE)) {
                List<SmsAssetFixedBorrowApplyProcessDetailVO> filterList = applyVO.getProcessList()
                        .stream()
                        .filter(fillProcess -> fillProcess.getProcessDetailId().equals(process.getId()))
                        .collect(Collectors.toList());
                if (filterList.size() < 1) {
                    ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.PAYMENT_ALLOW_PROCESS_NOT_FILL));
                }
                SmsAssetFixedBorrowApplyProcessDetailVO fillProcessReq = filterList.get(0);

                if (isNullOrEmpty(fillProcessReq.getProcessDetailObjectId())) {
                    ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
                }
                addDetail.setProcessDetailObjectId(fillProcessReq.getProcessDetailObjectId());
                addDetail.setProcessDetailObjectType(fillProcessReq.getProcessDetailObjectType());
            } else {
                addDetail.setProcessDetailObjectId(process.getProcessDetailObjectId());
                addDetail.setProcessDetailObjectType(process.getProcessDetailObjectType());
            }
            smsAssetFixedBorrowApplyProcessDetailMapper.insert(addDetail);
        }


        //消息通知
        //通知流程第一个节点
        SmsAssetFixedBorrowApplyProcessDetail firstDetail =
                smsAssetFixedBorrowApplyProcessDetailMapper.selectOne(new QueryWrapper<SmsAssetFixedBorrowApplyProcessDetail>()
                        .lambda()
                        .eq(SmsAssetFixedBorrowApplyProcessDetail::getApplyId, apply_id)
                        .eq(SmsAssetFixedBorrowApplyProcessDetail::getProcessDetailSort, 1)
                        .orderByAsc(SmsAssetFixedBorrowApplyProcessDetail::getProcessDetailSort));
        RangeTypeEnum rangeTypeEnum;
        if (firstDetail.getProcessDetailIsApplicantEdit().equals(1)) {
            rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_PERSON;
        } else {
            if (firstDetail.getProcessDetailObjectType() == ASSET_BORROW_OBJECT_TYPE_PERSON) {
                rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_PERSON;
            } else {
                rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_ROLE;
            }
        }

        List<String> rangeList = new ArrayList<>();
        rangeList.add(firstDetail.getProcessDetailObjectId());

        newTipsUtil.addNewTips(TipsTypeEnum.TIPS_TYPE_ENUM_ALLOW,
                user_id,
                rangeTypeEnum,
                rangeList,
                AppTypeEnum.APP_TYPE_ENUM_ASSET,
                apply_id,
                firstDetail.getId());
        return CommonResult.success();
    }

    @Transactional
    @Override
    public ResponseResult smsAssetFixedBorrowApplyCancel(SmsAssetFixedBorrowApplyVO cancelVO, String user_id) {
        if (isNullOrEmpty(cancelVO.getApplyId())) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
        }
        SmsAssetFixedBorrowApply assetBorrowApply = smsAssetFixedBorrowApplyMapper.selectOne(new QueryWrapper<SmsAssetFixedBorrowApply>()
                .lambda()
                .eq(SmsAssetFixedBorrowApply::getApplyUserId, user_id)
                .eq(SmsAssetFixedBorrowApply::getId, cancelVO.getApplyId()));
        if (assetBorrowApply == null) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_APPLY_NOT_EXIST));
        }
        if (assetBorrowApply.getAllowStatus() != ASSET_BORROW_ALLOW_STATUS_WAIT_ALLOW) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_CANCEL_FAIL_ALLOWED));
        }
        //判断是否审批中
        List<SmsAssetFixedBorrowApplyProcessDetail> processDetailList =
                smsAssetFixedBorrowApplyProcessDetailMapper.selectList(new QueryWrapper<SmsAssetFixedBorrowApplyProcessDetail>()
                        .lambda()
                        .eq(SmsAssetFixedBorrowApplyProcessDetail::getApplyId, cancelVO.getApplyId())
                        .eq(SmsAssetFixedBorrowApplyProcessDetail::getProcessDetailStatus, ASSET_BORROW_ALLOW_STATUS_PASS));
        if (processDetailList.size() > 0 && processDetailList != null) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_CANCEL_FAIL_ALLOWED));
        }
        //撤销申请
        SmsAssetFixedBorrowApply cancelApply = new SmsAssetFixedBorrowApply();
        cancelApply.setId(cancelVO.getApplyId());
        cancelApply.setAllowStatus(ASSET_BORROW_ALLOW_STATUS_CANCEL);
        cancelApply.setEndTime(new Date());
        smsAssetFixedBorrowApplyMapper.updateById(cancelApply);

        //修改节点状态
        SmsAssetFixedBorrowApplyProcessDetail cancelProcessDetail = new SmsAssetFixedBorrowApplyProcessDetail();
        cancelProcessDetail.setProcessDetailStatus(ASSET_BORROW_ALLOW_STATUS_CANCEL);
        smsAssetFixedBorrowApplyProcessDetailMapper.update(cancelProcessDetail, new QueryWrapper<SmsAssetFixedBorrowApplyProcessDetail>()
                .lambda()
                .eq(SmsAssetFixedBorrowApplyProcessDetail::getApplyId, cancelVO.getApplyId()));
        //消息通知
        newTipsUtil.cancelFinishTips(cancelVO.getApplyId());
        return CommonResult.success();
    }

    @Transactional
    @Override
    public ResponseResult getAssetFixedBorrowApplyList(Integer pageSize, Integer pageNum, Integer apply_type, String user_id) {
        if (isNullOrEmpty(apply_type)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<SmsAssetFixedBorrowApplyDTO> respList = new ArrayList<>();
        int total = 0;
        //查看已发起
        if (apply_type == ASSET_BORROW_APPLY_TYPE_APPLY) {
            Page<SmsAssetFixedBorrowApply> page = new Page<>(pageNum, pageSize);
            QueryWrapper<SmsAssetFixedBorrowApply> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SmsAssetFixedBorrowApply::getApplyUserId, user_id)
                    .orderByDesc(SmsAssetFixedBorrowApply::getCreateTime);
            smsAssetFixedBorrowApplyMapper.selectPage(page, queryWrapper);
            respList = page.getRecords()
                    .stream()
                    .map(apply -> {
                        SmsAssetFixedBorrowApplyDTO borrowApplyDTO =
                                new SmsAssetFixedBorrowApplyDTO();
                        BeanUtils.copyProperties(apply, borrowApplyDTO);
                        borrowApplyDTO.setApplyId(apply.getId());
                        SmsUser smsUser = cacheUtil.getUserInfo(apply.getApplyUserId());
                        SmsSearchUserResp userResp = new SmsSearchUserResp();
                        if (!isNullOrEmpty(smsUser)) {
                            BeanUtils.copyProperties(smsUser, userResp);
                        }
                        borrowApplyDTO.setApplyUserInfo(userResp);
                        return borrowApplyDTO;
                    })
                    .collect(Collectors.toList());
        }
        //查看已办类型
        else if (apply_type == ASSET_BORROW_APPLY_TYPE_ALLOWED) {
            Page<SmsAssetFixedBorrowApplyMPJDTO> page = new Page<>(pageNum, pageSize);
            MPJLambdaWrapper<SmsAssetFixedBorrowApplyProcessDetail> mpjLambdaWrapper = new MPJLambdaWrapper<>();
            mpjLambdaWrapper.selectAll(SmsAssetFixedBorrowApplyProcessDetail.class)
                    .selectAll(SmsAssetFixedBorrowApply.class)
                    .selectAs(SmsAssetFixedBorrowApply::getCreateTime, "applyCreateTime")
                    .selectAs(SmsAssetFixedBorrowApplyProcessDetail::getId, "detailId")
                    .leftJoin(SmsAssetFixedBorrowApply.class, SmsAssetFixedBorrowApply::getId, SmsAssetFixedBorrowApplyProcessDetail::getApplyId)
                    .eq(SmsAssetFixedBorrowApplyProcessDetail::getAllowUserId, user_id)
                    //.orderByDesc(SmsAssetFixedBorrowApply::getCreateTime)
                    .orderByDesc(SmsAssetFixedBorrowApplyProcessDetail::getEndTime);
            smsAssetFixedBorrowApplyProcessDetailMPJMapper.selectJoinPage(page, SmsAssetFixedBorrowApplyMPJDTO.class, mpjLambdaWrapper);
            respList = page.getRecords()
                    .stream()
                    .map(smsMPJResp -> {
                        SmsAssetFixedBorrowApplyDTO applyResp = new SmsAssetFixedBorrowApplyDTO();
                        BeanUtils.copyProperties(smsMPJResp, applyResp);
                        applyResp.setCreateTime(smsMPJResp.getApplyCreateTime());
                        applyResp.setEndTime(smsMPJResp.getEndTime());
                        SmsUser smsUser = cacheUtil.getUserInfo(smsMPJResp.getAllowUserId());
                        SmsSearchUserResp allowUserResp = new SmsSearchUserResp();
                        if (!isNullOrEmpty(smsUser)) {
                            BeanUtils.copyProperties(smsUser, allowUserResp);
                        }
                        applyResp.setApplyUserInfo(allowUserResp);

                        return applyResp;
                    }).collect(Collectors.toList());
            total = (int) page.getTotal();
        } else if (apply_type == ASSET_BORROW_APPLY_TYPE_ALLOW_WAIT_ME) {
            List<String> roleList = cacheUtil.getRoleList(user_id);
            //获取所有待审批申请流程节点
            List<SmsAssetFixedBorrowApplyProcessDetail> allDetailList =
                    smsAssetFixedBorrowApplyProcessDetailMapper.selectList(new QueryWrapper<SmsAssetFixedBorrowApplyProcessDetail>()
                            .lambda()
                            .eq(SmsAssetFixedBorrowApplyProcessDetail::getProcessDetailStatus, ASSET_BORROW_ALLOW_STATUS_WAIT_ALLOW));

            List<SmsAssetFixedBorrowApplyProcessDetail> filterDetailList = allDetailList.stream()
                    .filter(detail -> {
                        if (detail.getProcessDetailIsApplicantEdit().equals(1)) {
                            if (!detail.getProcessDetailObjectId().equals(user_id)) {
                                return false;
                            }
                        } else {
                            if (detail.getProcessDetailObjectType() == ASSET_BORROW_OBJECT_TYPE_PERSON) {
                                if (!detail.getProcessDetailObjectId().equals(user_id)) {
                                    return false;
                                }
                            } else {
                                List<String> filterRoleList = roleList.stream().filter(role -> role.equals(detail.getProcessDetailObjectId())).collect(Collectors.toList());
                                if (filterRoleList.size() < 1) {
                                    return false;
                                }
                            }
                        }


                        if (detail.getProcessDetailSort().equals(1)) {
                            return true;
                        }
                        //判断上一个流程节点是否完成
                        SmsAssetFixedBorrowApplyProcessDetail preDetail =
                                smsAssetFixedBorrowApplyProcessDetailMapper.selectOne(new QueryWrapper<SmsAssetFixedBorrowApplyProcessDetail>()
                                        .lambda()
                                        .eq(SmsAssetFixedBorrowApplyProcessDetail::getApplyId, detail.getApplyId())
                                        .eq(SmsAssetFixedBorrowApplyProcessDetail::getProcessDetailSort, detail.getProcessDetailSort() - 1)
                                        .eq(SmsAssetFixedBorrowApplyProcessDetail::getProcessDetailStatus, ASSET_BORROW_ALLOW_STATUS_PASS));
                        if (preDetail == null || isNullOrEmpty(preDetail.getId())) {
                            return false;
                        }
                        return true;
                    })
                    .collect(Collectors.toList());
            if (filterDetailList.size() > 0) {
                List<String> detailIDList = filterDetailList.stream()
                        .map(SmsAssetFixedBorrowApplyProcessDetail::getId)
                        .collect(Collectors.toList());

                Page<SmsAssetFixedBorrowApplyMPJDTO> page = new Page<>(pageNum, pageSize);
                MPJLambdaWrapper<SmsAssetFixedBorrowApplyProcessDetail> mpjLambdaWrapper = new MPJLambdaWrapper<>();
                mpjLambdaWrapper.selectAll(SmsAssetFixedBorrowApplyProcessDetail.class)
                        .selectAll(SmsAssetFixedBorrowApply.class)
                        .selectAs(SmsAssetFixedBorrowApply::getCreateTime, "applyCreateTime")
                        .selectAs(SmsAssetFixedBorrowApplyProcessDetail::getId, "detailId")
                        .leftJoin(SmsAssetFixedBorrowApply.class, SmsAssetFixedBorrowApply::getId, SmsAssetFixedBorrowApplyProcessDetail::getApplyId)
                        .in(SmsAssetFixedBorrowApplyProcessDetail::getId, detailIDList)
                        .orderByDesc(SmsAssetFixedBorrowApply::getCreateTime)
                        .orderByDesc(SmsAssetFixedBorrowApplyProcessDetail::getCreateTime)
                ;
                smsAssetFixedBorrowApplyProcessDetailMPJMapper.selectJoinPage(page, SmsAssetFixedBorrowApplyMPJDTO.class, mpjLambdaWrapper);

                respList = page.getRecords()
                        .stream()
                        .map(smsApplyMPJResp -> {
                            SmsAssetFixedBorrowApplyDTO applyResp = new SmsAssetFixedBorrowApplyDTO();
                            BeanUtils.copyProperties(smsApplyMPJResp, applyResp);
                            applyResp.setCreateTime(smsApplyMPJResp.getApplyCreateTime());

                            SmsUser applyUser = cacheUtil.getUserInfo(smsApplyMPJResp.getApplyUserId());
                            SmsSearchUserResp smsExamineUserResp = new SmsSearchUserResp();
                            if (applyUser != null) {
                                BeanUtils.copyProperties(applyUser, smsExamineUserResp);
                            }
                            applyResp.setApplyUserInfo(smsExamineUserResp);

                            return applyResp;
                        })
                        .collect(Collectors.toList());
                total = (int) page.getTotal();
            }
        }

        return CommonResult.success(respList, total);

    }

    @Transactional
    @Override
    public ResponseResult getAssetFixedBorrowApplyDetail(String apply_id, String user_id) {
        if (isNullOrEmpty(apply_id)) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
        }
        SmsAssetFixedBorrowApply assetApply = smsAssetFixedBorrowApplyMapper.selectById(apply_id);
        if (assetApply == null || isNullOrEmpty(assetApply.getId())) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_APPLY_NOT_EXIST));
        }
        SmsAssetFixedBorrowApplyDTO applyDTO = new SmsAssetFixedBorrowApplyDTO();
        BeanUtils.copyProperties(assetApply, applyDTO);
        applyDTO.setApplyId(assetApply.getId());
        SmsUser smsUser = cacheUtil.getUserInfo(applyDTO.getApplyUserId());
        SmsSearchUserResp smsUserResp = new SmsSearchUserResp();
        if (smsUser != null) {
            BeanUtils.copyProperties(smsUser, smsUserResp);
        }
        applyDTO.setApplyUserInfo(smsUserResp);

        //封装流程节点
        List<SmsAssetFixedBorrowApplyProcessDetailDTO> detailList =
                smsAssetFixedBorrowApplyProcessDetailMapper.selectList(new QueryWrapper<SmsAssetFixedBorrowApplyProcessDetail>()
                                .lambda()
                                .eq(SmsAssetFixedBorrowApplyProcessDetail::getApplyId, apply_id)
                                .orderByAsc(SmsAssetFixedBorrowApplyProcessDetail::getProcessDetailSort))
                        .stream()
                        .map(detail -> {
                            SmsAssetFixedBorrowApplyProcessDetailDTO detailDTO = new SmsAssetFixedBorrowApplyProcessDetailDTO();
                            BeanUtils.copyProperties(detail, detailDTO);
                            if (!isNullOrEmpty(detailDTO.getAllowUserId())) {
                                SmsUser userInfo = cacheUtil.getUserInfo(detailDTO.getAllowUserId());
                                SmsSearchUserResp smsSearchUserResp = new SmsSearchUserResp();
                                if (userInfo != null) {
                                    BeanUtils.copyProperties(userInfo, smsSearchUserResp);
                                }
                                detailDTO.setAllowUserInfo(smsSearchUserResp);
                            }
                            return detailDTO;
                        })
                        .collect(Collectors.toList());
        applyDTO.setProcessList(detailList);
        //封装物品信息
        List<SmsAssetFixedBorrowApplyGoodsDTO> goodsList =
                smsAssetFixedBorrowApplyGoodsDetailMapper.selectList(new QueryWrapper<SmsAssetFixedBorrowApplyGoodsDetail>()
                                .lambda()
                                .eq(SmsAssetFixedBorrowApplyGoodsDetail::getApplyId, apply_id))
                        .stream()
                        .map(goodsDetail -> {
                            SmsAssetFixedBorrowApplyGoodsDTO goodsDTO = new SmsAssetFixedBorrowApplyGoodsDTO();
                            SmsAssetFixedStorage fixedStorage = smsAssetFixedStorageMapper.getFixedStorageByID(goodsDetail.getAssetId());
                            if (fixedStorage != null) {
                                BeanUtils.copyProperties(fixedStorage, goodsDTO);
                            }

//                            List<String> assetPreIdList = smsAssetFixedBorrowApplyGoodsDetailMapper.selectList(new QueryWrapper<SmsAssetFixedBorrowApplyGoodsDetail>()
//                                    .lambda()
//                                    .eq(SmsAssetFixedBorrowApplyGoodsDetail::getAssetGoodsId, goods.getId()))
//                                    .stream()
//                                    .map(SmsAssetFixedBorrowApplyGoodsDetail::getAssetId)
//                                    .collect(Collectors.toList());

                            SmsAssetCategoryManagement categoryManagement = smsAssetCategoryManagementMapper.selectById(fixedStorage.getAssetCategory());
                            if (categoryManagement != null) {
                                goodsDTO.setAssetCategoryName(categoryManagement.getCategoryName());
                            }
//                            Integer count = smsAssetFixedStorageMapper.selectCount(new QueryWrapper<SmsAssetFixedStorage>()
//                                    .lambda()
//                                    .eq(SmsAssetFixedStorage::getAssetStatus,ASSET_STATUS_IN_STORAGE)
//                                    .eq(SmsAssetFixedStorage::getAssetName, goods.getAssetName())
//                                    .eq(SmsAssetFixedStorage::getAssetCategory, goods.getAssetCategory())
//                                    .eq(SmsAssetFixedStorage::getAssetBrand, goods.getAssetBrand())
//                                    .eq(SmsAssetFixedStorage::getAssetModel, goods.getAssetModel())
//                                    .eq(SmsAssetFixedStorage::getAssetPrice, goods.getAssetPrice()));
//                            goodsDTO.setAssetCount(count);
//                            goodsDTO.setAssetAssetIdList(assetPreIdList);
                            return goodsDTO;
                        })
                        .collect(Collectors.toList());
        applyDTO.setGoodsList(goodsList);
        return CommonResult.success(applyDTO);
    }

    @Transactional
    @Override
    public ResponseResult smsAssetFixedBorrowApplyAllow(SmsAssetFixedBorrowApplyAllowVO allowVO, String user_id) {
        if (isNullOrEmpty(allowVO.getApplyId()) || isNullOrEmpty(allowVO.getDetailId())
                || isNullOrEmpty(allowVO.getProcessDetailStatus())) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
        }
        if (allowVO.getProcessDetailStatus() != ASSET_BORROW_ALLOW_STATUS_PASS
                && allowVO.getProcessDetailStatus() != ASSET_BORROW_ALLOW_STATUS_DENY) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
        }
        SmsAssetFixedBorrowApply allowApply = smsAssetFixedBorrowApplyMapper.selectById(allowVO.getApplyId());
        if (isNullOrEmpty(allowApply)) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_APPLY_NOT_EXIST));
        }
        SmsAssetFixedBorrowApplyProcessDetail processDetail =
                smsAssetFixedBorrowApplyProcessDetailMapper.selectOne(new QueryWrapper<SmsAssetFixedBorrowApplyProcessDetail>()
                        .lambda()
                        .eq(SmsAssetFixedBorrowApplyProcessDetail::getApplyId, allowVO.getApplyId())
                        .eq(SmsAssetFixedBorrowApplyProcessDetail::getId, allowVO.getDetailId())
                        .eq(SmsAssetFixedBorrowApplyProcessDetail::getProcessDetailStatus, ASSET_BORROW_ALLOW_STATUS_WAIT_ALLOW));
        if (isNullOrEmpty(processDetail.getId()) || processDetail == null) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_ALLOW_FAIL));
        }
        SmsAssetFixedBorrowApplyProcessDetail allowDetail = new SmsAssetFixedBorrowApplyProcessDetail();
        allowDetail.setId(allowVO.getDetailId());
        allowDetail.setProcessDetailStatus(allowVO.getProcessDetailStatus());
        if (!isNullOrEmpty(allowVO.getProcessDetailRemark())) {
            allowDetail.setProcessDetailRemark(allowVO.getProcessDetailRemark());
        }

        Date date = new Date();
        allowDetail.setEndTime(date);
        allowDetail.setAllowUserId(user_id);
        if (processDetail.getProcessDetailIsSign() == ASSET_BORROW_BOOLEAN_TRUE) {
            if (isNullOrEmpty(allowVO.getProcessDetailSign())) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
            }
            allowDetail.setProcessDetailSign(allowVO.getProcessDetailSign());
        }
        if (processDetail.getProcessDetailIsStamp() == ASSET_BORROW_BOOLEAN_TRUE) {
            if (isNullOrEmpty(allowVO.getProcessDetailStamp())) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.INVALID_PARAM));
            }
            allowDetail.setProcessDetailStamp(allowVO.getProcessDetailStamp());
        }
        smsAssetFixedBorrowApplyProcessDetailMapper.updateById(allowDetail);

        boolean is_done = false;

        //判断是否最后一个节点 是否驳回
        Integer detailCount = smsAssetFixedBorrowApplyProcessDetailMapper.selectCount(new QueryWrapper<SmsAssetFixedBorrowApplyProcessDetail>()
                .lambda()
                .eq(SmsAssetFixedBorrowApplyProcessDetail::getApplyId, allowVO.getApplyId()));
        if (detailCount.equals(processDetail.getProcessDetailSort()) || allowVO.getProcessDetailStatus() == ASSET_BORROW_ALLOW_STATUS_DENY) {
            is_done = true;

            SmsAssetFixedBorrowApply updateApply = new SmsAssetFixedBorrowApply();
            updateApply.setId(allowVO.getApplyId());
            updateApply.setEndTime(date);
            updateApply.setAllowStatus(allowVO.getProcessDetailStatus());
            smsAssetFixedBorrowApplyMapper.updateById(updateApply);
        }

        //流程结束并且同意，从资产库把对应的资产修改对应状态
        Date now_Date = new Date();
        //流程最后一个人审批完成并且审批通过
        if (detailCount.equals(processDetail.getProcessDetailSort()) && allowVO.getProcessDetailStatus() == ASSET_BORROW_ALLOW_STATUS_PASS) {
//            List<SmsAssetFixedBorrowApplyGoods> goodsList =
//                    smsAssetFixedBorrowApplyGoodsMapper.selectList(new QueryWrapper<SmsAssetFixedBorrowApplyGoods>()
//                            .lambda()
//                            .eq(SmsAssetFixedBorrowApplyGoods::getApplyId, allowVO.getApplyId()));
//            if (goodsList != null && goodsList.size() > 0) {
//                for (SmsAssetFixedBorrowApplyGoods goods : goodsList) {
//                    Integer count = smsAssetFixedStorageMapper.selectCount(new QueryWrapper<SmsAssetFixedStorage>()
//                            .lambda()
//                            .eq(SmsAssetFixedStorage::getAssetStatus,1)
//                            .eq(SmsAssetFixedStorage::getAssetName, goods.getAssetName())
//                            .eq(SmsAssetFixedStorage::getAssetCategory, goods.getAssetCategory())
//                            .eq(SmsAssetFixedStorage::getAssetBrand, goods.getAssetBrand())
//                            .eq(SmsAssetFixedStorage::getAssetModel, goods.getAssetModel())
//                            .eq(SmsAssetFixedStorage::getAssetPrice, goods.getAssetPrice()));
//                    if (count >= goods.getAssetNum()) {
//                        Page<SmsAssetFixedStorage> page = new Page<>(1, goods.getAssetNum());
//                        smsAssetFixedStorageMapper.selectPage(page, new QueryWrapper<SmsAssetFixedStorage>()
//                                .lambda()
//                                .eq(SmsAssetFixedStorage::getAssetStatus, 1)
//                                .eq(SmsAssetFixedStorage::getAssetName, goods.getAssetName())
//                                .eq(SmsAssetFixedStorage::getAssetCategory, goods.getAssetCategory())
//                                .eq(SmsAssetFixedStorage::getAssetBrand, goods.getAssetBrand())
//                                .eq(SmsAssetFixedStorage::getAssetModel, goods.getAssetModel())
//                                .eq(SmsAssetFixedStorage::getAssetPrice, goods.getAssetPrice()));
//                        List<SmsAssetFixedStorage> list = page.getRecords();
//                        for (SmsAssetFixedStorage storage : list) {
//                            SmsAssetFixedBorrowApplyGoodsDetail goodsDetail = new SmsAssetFixedBorrowApplyGoodsDetail();
//                            goodsDetail.setAssetGoodsId(goods.getId());
//                            goodsDetail.setAssetId(storage.getId());
//                            goodsDetail.setApplyId(allowVO.getApplyId());
//                            smsAssetFixedBorrowApplyGoodsDetailMapper.insert(goodsDetail);
//                            storage.setAssetStatus(2);
//                            smsAssetFixedStorageMapper.updateById(storage);
//
//                            SmsAssetMyAssets smsAssetMyAssets1 = new SmsAssetMyAssets();
//                            smsAssetMyAssets1.setAssetId(storage.getId());
//                            QueryWrapper<SmsAssetFixedBorrowApply> queryWrapper = new QueryWrapper<>();
//                            queryWrapper.lambda()
//                                    .eq(SmsAssetFixedBorrowApply::getId, allowVO.getApplyId());
//                            SmsAssetFixedBorrowApply query = smsAssetFixedBorrowApplyMapper.selectOne(queryWrapper);
//                            smsAssetMyAssets1.setUserId(query.getApplyUserId());
//                            smsAssetMyAssets1.setAssetStatus(2);
//                            smsAssetMyAssetsMapper.insert(smsAssetMyAssets1);
//
//                        }
//
//
//                    } else {
//                        ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_ASSET_NUM_NOT_ENOUGH));
//                    }
//                }
//
//            }

            //获取资产列表，并判断资产当前状态
            List<SmsAssetFixedBorrowApplyGoodsDetail> goodsDetailList = smsAssetFixedBorrowApplyGoodsDetailMapper.selectList(new LambdaQueryWrapper<SmsAssetFixedBorrowApplyGoodsDetail>()
                    .eq(SmsAssetFixedBorrowApplyGoodsDetail::getApplyId, allowVO.getApplyId()));
            if (goodsDetailList == null || goodsDetailList.size() < 1) {
                ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.FAIL));
            }
            for (SmsAssetFixedBorrowApplyGoodsDetail goodsDetail : goodsDetailList) {
                SmsAssetFixedStorage fixedStorage = smsAssetFixedStorageMapper.selectById(goodsDetail.getAssetId());
                if (fixedStorage.getAssetStatus() != ASSET_STATUS_IN_STORAGE) {
                    ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.ASSET_BORROW_STORAGE_NOT_IN_STORAGE));
                }

                //存入我的资产  修改资产在库状态
                SmsAssetMyAssets addMyAssets = new SmsAssetMyAssets();
                addMyAssets.setMyAssetStatus(MY_ASSET_STATUS_BORROW);
                addMyAssets.setAssetId(goodsDetail.getAssetId());
                addMyAssets.setUserId(allowApply.getApplyUserId());
                addMyAssets.setBorrowTime(new Date());
                smsAssetMyAssetsMapper.insert(addMyAssets);

                SmsAssetFixedStorage updateFixedStorage = new SmsAssetFixedStorage();
                updateFixedStorage.setId(goodsDetail.getAssetId());
                updateFixedStorage.setAssetStatus(ASSET_STATUS_BORROW);
                updateFixedStorage.setAssetOwner(allowApply.getApplyUserId());
                smsAssetFixedStorageMapper.updateById(updateFixedStorage);
            }
        }
        //节点驳回 重置其余节点状态
        if (allowVO.getProcessDetailStatus() == ASSET_BORROW_ALLOW_STATUS_DENY) {
            SmsAssetFixedBorrowApplyProcessDetail updateDenyDetail =
                    new SmsAssetFixedBorrowApplyProcessDetail();
            updateDenyDetail.setProcessDetailStatus(ASSET_BORROW_ALLOW_STATUS_DENY_FINISH);
            smsAssetFixedBorrowApplyProcessDetailMapper.update(updateDenyDetail, new QueryWrapper<SmsAssetFixedBorrowApplyProcessDetail>()
                    .lambda()
                    .eq(SmsAssetFixedBorrowApplyProcessDetail::getApplyId, allowVO.getApplyId())
                    .eq(SmsAssetFixedBorrowApplyProcessDetail::getProcessDetailStatus, ASSET_BORROW_ALLOW_STATUS_WAIT_ALLOW));
        }

        //消息通知
        if (is_done) {
            boolean result_status = false;
            if (allowVO.getProcessDetailStatus() == ASSET_BORROW_ALLOW_STATUS_PASS) {
                result_status = true;
            }
            newTipsUtil.allowFinishTips(allowVO.getApplyId(), user_id, result_status);
        } else {
            SmsAssetFixedBorrowApplyProcessDetail nextDetail =
                    smsAssetFixedBorrowApplyProcessDetailMapper.selectOne(new QueryWrapper<SmsAssetFixedBorrowApplyProcessDetail>()
                            .lambda()
                            .eq(SmsAssetFixedBorrowApplyProcessDetail::getApplyId, allowVO.getApplyId())
                            .eq(SmsAssetFixedBorrowApplyProcessDetail::getProcessDetailSort, processDetail.getProcessDetailSort() + 1));
            RangeTypeEnum rangeTypeEnum;
            if (nextDetail.getProcessDetailIsApplicantEdit().equals(1)) {
                rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_PERSON;
            } else {
                if (nextDetail.getProcessDetailObjectType() == ASSET_BORROW_OBJECT_TYPE_ROLE) {
                    rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_ROLE;
                } else {
                    rangeTypeEnum = RangeTypeEnum.RANGE_TYPE_ENUM_PERSON;
                }
            }

            List<String> rangeIDList = new ArrayList<>();
            rangeIDList.add(nextDetail.getProcessDetailObjectId());
            newTipsUtil.allowContinueTips(allowVO.getApplyId(),
                    user_id,
                    rangeTypeEnum,
                    rangeIDList,
                    nextDetail.getId());
        }
        return CommonResult.success();
    }

    @Override
    public ResponseResult selectAllAssetFixedBorrowApply(String apply_person, String apply_department, Integer pageSize, Integer pageNum) {
        List<SmsAssetFixedBorrowApply> list11 = new ArrayList<>();
        QueryWrapper<SmsAssetFixedBorrowApply> queryWrapper = new QueryWrapper<>();
        if (isNullOrEmpty(queryWrapper)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }

        if (!isNullOrEmpty(apply_person)) {
            queryWrapper.lambda().like(SmsAssetFixedBorrowApply::getApplyPerson, apply_person);
        }
        if (!isNullOrEmpty(apply_department)) {
            queryWrapper.lambda().like(SmsAssetFixedBorrowApply::getApplyDepartment, apply_department);
        }

        List<SmsAssetFixedBorrowApply> list1 = smsAssetFixedBorrowApplyMapper.selectList(queryWrapper);
        if (list1.size() == 0) {
            return CommonResult.success(list11, 0);
        }
        queryWrapper.orderByDesc("create_time");
        Page<SmsAssetFixedBorrowApply> page = new Page<>(pageNum, pageSize);
        smsAssetFixedBorrowApplyMapper.selectPage(page, queryWrapper);
        list11 = page.getRecords();
        int total = (int) page.getTotal();
        return CommonResult.success(list11, total);


    }

    @Override
    public ResponseResult selectDetail(String id) {
        QueryWrapper<SmsAssetFixedBorrowApplyGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SmsAssetFixedBorrowApplyGoods::getApplyId, id);
        SmsAssetFixedBorrowApplyGoods detail = smsAssetFixedBorrowApplyGoodsMapper.selectOne(queryWrapper);
        if (isNullOrEmpty(detail)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }

        SmsAssetFixedBorrowApplyGoods smsAssetFixedBorrowApplyGoods = new SmsAssetFixedBorrowApplyGoods();
        smsAssetFixedBorrowApplyGoods.setApplyId(id);
        smsAssetFixedBorrowApplyGoodsMapper.updateById(smsAssetFixedBorrowApplyGoods);
        SmsAssetFixedBorrowApplyGoodsDTO smsAssetFixedBorrowApplyGoodsDTO = new SmsAssetFixedBorrowApplyGoodsDTO();
        BeanUtils.copyProperties(detail, smsAssetFixedBorrowApplyGoodsDTO);

        return CommonResult.success(smsAssetFixedBorrowApplyGoodsDTO);
    }

    @Override
    public ResponseResult select(String goodsId) {
        QueryWrapper<SmsAssetFixedBorrowApplyGoodsDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SmsAssetFixedBorrowApplyGoodsDetail::getAssetGoodsId, goodsId);
        List<SmsAssetFixedBorrowApplyGoodsDetail> list = smsAssetFixedBorrowApplyGoodsDetailMapper
                .selectList(queryWrapper);
        if (isNullOrEmpty(list) || list.size() < 1) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }

        return null;
    }

}